/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * frmOrder.java
 *
 * Created on 16-feb-2009, 11:52:15
 */
package gui.forms;

import gui.tools.Receiver;
import gui.tools.models.TMItem;
import java.awt.Image;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import lib.exceptions.InvalidLengthException;
import lib.model.FactoryModel;
import lib.plan.Item;
import lib.plan.Order;

/**
 *
 * @author jorge
 */
public class FrmOrder extends javax.swing.JDialog implements Receiver {

    private Order order = null;
    private FactoryModel factory;
    private boolean add;
    private int index;
    private Receiver parent;

    public FrmOrder(Order order, FactoryModel factory, Receiver parent) {
        initComponents();
        initDates();

        this.getRootPane().setDefaultButton(btnSave);

        String fileString = "/res/icons/logo.png";
        try {
            Image icon = ImageIO.read(getClass().getResource(fileString));
            this.setIconImage(icon);
        } catch (java.io.IOException ioe) {
            System.out.println("Error loading file: " + fileString);
        }

        this.parent = parent;
        this.factory = factory;
        tbItems.setModel(new TMItem(new ArrayList<Item>()));
        this.order = order;
        if (order != null) {
            this.setTitle("Editing order: " + order.getNumber());
            txtNumber.setText(String.valueOf(order.getNumber()));
            txtCustomer.setText(order.getCustomer());
            txtPriority.setText(String.valueOf(order.getPriority()));
            cbDateReceivedMonth.setSelectedIndex(
                    order.getDateReceived().get(Calendar.MONTH));
            cbDateReceivedDay.setSelectedIndex(
                    order.getDateReceived().get(Calendar.DATE) - 1);
            cbDateReceivedYear.setSelectedIndex(
                    order.getDateReceived().get(Calendar.YEAR) - 2008);
            txtDateReceivedHour.setText(String.valueOf(
                    order.getDateReceived().get(Calendar.HOUR_OF_DAY)));
            txtDateReceivedMinute.setText(String.valueOf(
                    order.getDateReceived().get(Calendar.MINUTE)));
            cbDateDueMonth.setSelectedIndex(
                    order.getDateDue().get(Calendar.MONTH));
            cbDateDueDay.setSelectedIndex(
                    order.getDateDue().get(Calendar.DATE) - 1);
            cbDateDueYear.setSelectedIndex(
                    order.getDateDue().get(Calendar.YEAR) - 2008);
            txtDateDueHour.setText(String.valueOf(
                    order.getDateDue().get(Calendar.HOUR_OF_DAY)));
            txtDateDueMinute.setText(String.valueOf(
                    order.getDateDue().get(Calendar.MINUTE)));
            tbItems.setModel(new TMItem(order.getItems()));
        } else {
            this.setTitle("New order:");
            Calendar calendar = Calendar.getInstance();
            cbDateReceivedMonth.setSelectedIndex(calendar.get(Calendar.MONTH));
            cbDateReceivedDay.setSelectedIndex(calendar.get(Calendar.DATE) - 1);
            cbDateReceivedYear.setSelectedIndex(calendar.get(Calendar.YEAR) - 2008);
            cbDateDueMonth.setSelectedIndex(calendar.get(Calendar.MONTH));
            cbDateDueDay.setSelectedIndex(calendar.get(Calendar.DATE) - 1);
            cbDateDueYear.setSelectedIndex(calendar.get(Calendar.YEAR) - 2008);
        }
    }

    private void initDates() {
        for (int i = 1; i <= 12; i++) {
            cbDateReceivedMonth.addItem(i);
            cbDateDueMonth.addItem(i);
        }
        for (int i = 1; i <= 31; i++) {
            cbDateReceivedDay.addItem(i);
            cbDateDueDay.addItem(i);
        }
        for (int i = 2008; i <= 2020; i++) {
            cbDateReceivedYear.addItem(i);
            cbDateDueYear.addItem(i);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCancel = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        lblNumber = new javax.swing.JLabel();
        lblCustomer = new javax.swing.JLabel();
        lblDateReceived = new javax.swing.JLabel();
        lblDateDue = new javax.swing.JLabel();
        lblPriority = new javax.swing.JLabel();
        txtNumber = new javax.swing.JTextField();
        txtCustomer = new javax.swing.JTextField();
        txtPriority = new javax.swing.JTextField();
        cbDateReceivedMonth = new javax.swing.JComboBox();
        cbDateReceivedDay = new javax.swing.JComboBox();
        cbDateDueDay = new javax.swing.JComboBox();
        cbDateDueMonth = new javax.swing.JComboBox();
        cbDateReceivedYear = new javax.swing.JComboBox();
        cbDateDueYear = new javax.swing.JComboBox();
        spItems = new javax.swing.JScrollPane();
        tbItems = new javax.swing.JTable();
        lblItems = new javax.swing.JLabel();
        btnRemoveItem = new javax.swing.JButton();
        btnAddItem = new javax.swing.JButton();
        txtDateReceivedHour = new javax.swing.JTextField();
        txtDateDueHour = new javax.swing.JTextField();
        txtDateDueMinute = new javax.swing.JTextField();
        txtDateReceivedMinute = new javax.swing.JTextField();
        lblColon1 = new javax.swing.JLabel();
        lblColon2 = new javax.swing.JLabel();
        btnEditItem = new javax.swing.JButton();

        setModal(true);
        setResizable(false);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        lblNumber.setText("Number:");
        lblNumber.setEnabled(false);

        lblCustomer.setText("Customer:");

        lblDateReceived.setText("Date received:");

        lblDateDue.setText("Date due:");

        lblPriority.setText("Priority:");

        txtNumber.setEnabled(false);

        txtPriority.setText("1");

        cbDateDueDay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbDateDueDayActionPerformed(evt);
            }
        });

        cbDateDueYear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbDateDueYearActionPerformed(evt);
            }
        });

        tbItems.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        spItems.setViewportView(tbItems);

        lblItems.setText("Items:");

        btnRemoveItem.setText("Remove item");
        btnRemoveItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveItemActionPerformed(evt);
            }
        });

        btnAddItem.setText("Add item...");
        btnAddItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddItemActionPerformed(evt);
            }
        });

        txtDateReceivedHour.setColumns(2);
        txtDateReceivedHour.setText("00");

        txtDateDueHour.setColumns(2);
        txtDateDueHour.setText("00");

        txtDateDueMinute.setColumns(2);
        txtDateDueMinute.setText("00");

        txtDateReceivedMinute.setColumns(2);
        txtDateReceivedMinute.setText("00");

        lblColon1.setText(":");

        lblColon2.setText(":");

        btnEditItem.setText("Edit item...");
        btnEditItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditItemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(spItems, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnSave)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblDateReceived)
                            .addComponent(lblCustomer)
                            .addComponent(lblNumber)
                            .addComponent(lblItems)
                            .addComponent(lblPriority)
                            .addComponent(lblDateDue))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(cbDateReceivedMonth, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(cbDateDueMonth, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(cbDateReceivedDay, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(cbDateDueDay, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(cbDateDueYear, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(cbDateReceivedYear, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(txtDateDueHour)
                                            .addComponent(txtDateReceivedHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(lblColon1)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txtDateReceivedMinute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(lblColon2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txtDateDueMinute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addComponent(txtPriority, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtCustomer))
                                .addGap(99, 99, 99))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(btnAddItem)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnEditItem)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnRemoveItem)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNumber))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCustomer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCustomer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblDateReceived)
                            .addComponent(cbDateReceivedMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbDateDueMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblDateDue)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbDateReceivedDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbDateReceivedYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblColon1)
                            .addComponent(txtDateReceivedMinute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtDateReceivedHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbDateDueDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbDateDueYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblColon2)
                            .addComponent(txtDateDueHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtDateDueMinute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPriority, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPriority))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblItems)
                    .addComponent(btnAddItem)
                    .addComponent(btnEditItem)
                    .addComponent(btnRemoveItem))
                .addGap(18, 18, 18)
                .addComponent(spItems, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnSave))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-521)/2, (screenSize.height-538)/2, 521, 538);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if (setOrder()) {
            parent.reciveObject(order);
            this.setVisible(false);
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnAddItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddItemActionPerformed
        add = true;
        if (setOrder()) {
            new FrmItem(null, order, factory, this).setVisible(true);
        }
    }//GEN-LAST:event_btnAddItemActionPerformed

    private void btnRemoveItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveItemActionPerformed
        int row = tbItems.getSelectedRow();
        if (row >= 0) {
            int delete = JOptionPane.showConfirmDialog(this,
                    "Are you sure to delete the selected item?",
                    "Confirm your decision",
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.QUESTION_MESSAGE);
            if (delete == JOptionPane.CANCEL_OPTION) {
                return;
            }
            TMItem tmi = (TMItem) tbItems.getModel();
            tmi.removeItem(row);
            tbItems.setModel(tmi);
        } else {
            JOptionPane.showMessageDialog(rootPane,
                    "Error:\n Please select an item.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
    }//GEN-LAST:event_btnRemoveItemActionPerformed

    private void cbDateDueYearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbDateDueYearActionPerformed
    }//GEN-LAST:event_cbDateDueYearActionPerformed

    private void cbDateDueDayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbDateDueDayActionPerformed
    }//GEN-LAST:event_cbDateDueDayActionPerformed

    private void btnEditItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditItemActionPerformed
        index = tbItems.getSelectedRow();
        add = false;
        if (index >= 0) {
            TMItem tmItem = (TMItem) tbItems.getModel();
            Item item = tmItem.getItem(index);
            if (setOrder()) {
                new FrmItem(item, order, factory, this).setVisible(true);
            }
        } else {
            JOptionPane.showMessageDialog(rootPane,
                    "Error:\n Please select an item.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
    }//GEN-LAST:event_btnEditItemActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddItem;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnEditItem;
    private javax.swing.JButton btnRemoveItem;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbDateDueDay;
    private javax.swing.JComboBox cbDateDueMonth;
    private javax.swing.JComboBox cbDateDueYear;
    private javax.swing.JComboBox cbDateReceivedDay;
    private javax.swing.JComboBox cbDateReceivedMonth;
    private javax.swing.JComboBox cbDateReceivedYear;
    private javax.swing.JLabel lblColon1;
    private javax.swing.JLabel lblColon2;
    private javax.swing.JLabel lblCustomer;
    private javax.swing.JLabel lblDateDue;
    private javax.swing.JLabel lblDateReceived;
    private javax.swing.JLabel lblItems;
    private javax.swing.JLabel lblNumber;
    private javax.swing.JLabel lblPriority;
    private javax.swing.JScrollPane spItems;
    private javax.swing.JTable tbItems;
    private javax.swing.JTextField txtCustomer;
    private javax.swing.JTextField txtDateDueHour;
    private javax.swing.JTextField txtDateDueMinute;
    private javax.swing.JTextField txtDateReceivedHour;
    private javax.swing.JTextField txtDateReceivedMinute;
    private javax.swing.JTextField txtNumber;
    private javax.swing.JTextField txtPriority;
    // End of variables declaration//GEN-END:variables

    public void reciveObject(Object object) {
        Item item = (Item) object;
        TMItem tmi = (TMItem) tbItems.getModel();
        if (add) {
            tmi.addItem(item);
        } else {
            tmi.setItem(index, item);
        }
        tbItems.setModel(tmi);
        tbItems.tableChanged(new TableModelEvent(tbItems.getModel()));
    }

    private boolean setOrder() {
        if (Integer.parseInt(txtPriority.getText()) < 1) {
            JOptionPane.showMessageDialog(rootPane,
                    "Error:\n Priority must be bigger than zero.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (txtCustomer.getText().equals("")) {
            JOptionPane.showMessageDialog(rootPane,
                    "Error:\n Customer cannot be empty.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
            return false;
        }

        try {
            DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy-HH:mm");
            Date date;
            String string;

            //Date due
            string = cbDateReceivedMonth.getSelectedItem().toString() + "/" +
                    cbDateReceivedDay.getSelectedItem().toString() + "/" +
                    cbDateReceivedYear.getSelectedItem().toString() + "-" +
                    txtDateReceivedHour.getText() + ":" +
                    txtDateReceivedMinute.getText();
            date = (Date) formatter.parse(string);
            Calendar dateReceived = Calendar.getInstance();
            dateReceived.setTime(date);

            //Date recived
            string = cbDateDueMonth.getSelectedItem().toString() + "/" +
                    cbDateDueDay.getSelectedItem().toString() + "/" +
                    cbDateDueYear.getSelectedItem().toString() + "-" +
                    txtDateDueHour.getText() + ":" +
                    txtDateDueMinute.getText();
            date = (Date) formatter.parse(string);
            Calendar dateDue = Calendar.getInstance();
            dateDue.setTime(date);

            order = new Order(txtCustomer.getText(),
                    dateReceived,
                    dateDue,
                    Integer.parseInt(txtPriority.getText()));
            List<Item> items = ((TMItem) tbItems.getModel()).getItems();
            for (Item i : items) {
                order.addItem(i);
            }
            return true;
        } catch (InvalidLengthException ile) {
            JOptionPane.showMessageDialog(rootPane,
                    ile.getMessage(),
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        } catch (NumberFormatException nfe) {
            JOptionPane.showMessageDialog(rootPane,
                    "Error:\n Failed to parse dates, please check them again.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        } catch (ParseException pe) {
            JOptionPane.showMessageDialog(rootPane,
                    "Error:\n Failed to parse dates, please check them again.",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
        return false;
    }
}
